package com.huilian.security.sms.impl;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.impl.client.CloseableHttpClient;

import java.io.ByteArrayInputStream;

/**
 * Created by ${zhangfanyan} on 2017/6/10.
 */
public class SMSUtils {

    public String getSigParameter(String accountsid, String authtoken,
                                  String timestamp, EncryptUtil encryptUtil) throws Exception {
        String sig = accountsid + authtoken + timestamp;
        return encryptUtil.md5Digest(sig);
    }

    public String getAuth(String accountsid, String timestamp, EncryptUtil encryptUtil) throws Exception {
        String str = accountsid + ":" + timestamp;
        return encryptUtil.base64Encoder(str);
    }

    public HttpResponse post(String cType, String auth, String url, CloseableHttpClient httpclient, String body, HttpPost httppost) throws Exception{
        httppost.setHeader("Accept", cType);
        httppost.setHeader("Content-Type", cType+";charset=utf-8");
        httppost.setHeader("Authorization", auth);
        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(body.getBytes("UTF-8")));
        requestBody.setContentLength(body.getBytes("UTF-8").length);
        httppost.setEntity(requestBody);
        // 执行客户端请求
        HttpResponse response = httpclient.execute(httppost);
        return response;
    }
}
